package com.lxyk.controller.student;

//休学

import com.github.pagehelper.PageInfo;
import com.lxyk.domain.*;
import com.lxyk.service.student.CommonService;
import com.lxyk.service.student.QuitService;
import com.lxyk.service.user.ClassesService;
import com.lxyk.service.user.UserService;
import com.lxyk.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/quit")
public class QuitController {

    @Autowired
    private UserService userService;

    @Autowired
    private QuitService quitService;

    @Autowired
    private ClassesService classesService;

    //根据id查询学生休学信息
    @RequestMapping("/findById")
    public String findRefundById(Model model, Integer id) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        StudentVO3 student = quitService.findQuitById(id);
        model.addAttribute("student", student);
        model.addAttribute("username",username);
        return "student_quit";
    }

    //添加休学信息
    @RequestMapping("/addQuit")
    public String addQuit(StudentVO3 student) {
        quitService.addQuit(student);
        //修改学生标识
        quitService.updateStudent(student);
        //修改学生休学标识
        quitService.updateQuit(student);
        //修改班级人数
        classesService.suspension(student.getCid());
        return "forward:/common/findByCid";
    }

    //查询所有休息休息
    @RequestMapping("/findAllQuit")
    public String findAllQuit(Model model) {
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //根据用户名查询用户信息
        SysUser user = userService.findByName(username);
        //根据用户id查询对应班级
        List<Classes> list = classesService.findByUser(user.getId());
        ArrayList<Integer> counts = new ArrayList<>();
        for (Classes classes : list) {
            Integer id = classes.getId();
            int count = quitService.findCountByCid(id);
            counts.add(count);
        }
        model.addAttribute("counts", counts);
        model.addAttribute("list", list);
        return "quit_base";
    }

    //查询当前班级所有休学信息
    @RequestMapping("/quitByCid")
    public String quitByCid(Model model, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        PageInfo<StudentVO3> pageInfo = quitService.quitByCid(cid, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        model.addAttribute("username",username);
        return "quit_list";
    }

    //根据条件模糊查询休学信息
    @RequestMapping("/condition")
    public String findStudentByCondition(Model model, StudentVO3 vo, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        PageInfo<StudentVO3> pageInfo = quitService.findQuitByCondition(vo, pageNum, pageSize);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        model.addAttribute("name", vo.getName());
        model.addAttribute("applyTime", vo.getApplyTime());
        model.addAttribute("deadline", vo.getDeadline());
        return "quit_list";
    }
}
